//
//  ELSettingView.swift
//  WZApp
//
//  Created by evol on 2018/7/3.
//  Copyright © 2018年 evol. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import RxDataSources

class ELSettingView: UIButton {

    
    let disposeBag = DisposeBag()

    init(_ viewModel:ELSettingViewModel) {
        super.init(frame: .zero)
        setCloseEvent()
        
        //绿色背景
        let greenBackgroundImage = UIImage(named: "setting_bg_wz")?.stretchableImage(withLeftCapWidth: 15, topCapHeight: 15)
        let greenImageView = UIImageView(image: greenBackgroundImage)
        addSubview(greenImageView)
        greenImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
            make.width.equalTo(595.0/2.0)
        }
        
        // tableView
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.layer.cornerRadius = 3.00
        tableView.layer.masksToBounds = true
        tableView.isScrollEnabled = false
        tableView.register(UINib(nibName: "ELSettingNormalCell", bundle: nil), forCellReuseIdentifier: "ELSettingNormalCell")
        addSubview(tableView)
        tableView.rx.setDelegate(self).disposed(by:disposeBag)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalTo(greenImageView).inset(UIEdgeInsetsMake(12, 12, 12, 12))
            make.height.equalTo(0)
        }
        let dataSource = RxTableViewSectionedReloadDataSource<ELSettingSection>(configureCell:
            { ds, tv , ip , item in
                switch item {
                case let .imageNormal(title: title, image: image):
                    let cell = tv.dequeueReusableCell(withIdentifier: "ELSettingNormalCell") as! ELSettingNormalCell
                    cell.titleLabel.text = title
                    cell.rightImageView.image = image
                    return cell
                case .imageTurn(let title, let image, _):
                    let cell = tv.dequeueReusableCell(withIdentifier: "ELSettingNormalCell") as! ELSettingNormalCell
                    cell.titleLabel.text = title
                    cell.rightImageView.image = image
                    return cell
                }
        })
        
        viewModel.sections.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
        
        _ = tableView.rx.observe(CGSize.self, "contentSize").subscribe(onNext: { (size) in
            let height = size?.height
            tableView.snp.updateConstraints({ (make) in
                make.height.equalTo(height!)
            })
        })
    }

    
    func setCloseEvent() -> Void {
        self.rx.tap.subscribe(onNext:{ [weak self] _ in
            self?.removeFromSuperview()
        }).disposed(by: disposeBag)
    }
    
    deinit {
        print("settingview deinit")
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension ELSettingView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 0.1
        }
        return 5.0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0.1))
        return view
    }
}
